<?php
class m_sekolah extends Model{
    var $table = "sekolah";
    
    function getCountRows(){
        return $this->db->count_all($this->table);
    }
    function getListSekolah($offset = NULL,$limit = NULL){
        $query = $this->db->query("select s.*,tp.nama as tingkat_pendidikan from sekolah s join tingkat_pendidikan tp on s.id_tingkat_pendidikan=tp.id limit $offset,$limit");
        if($query->num_rows() > 0){
            $result = $query->result_array();
        }else $result = array();
        
        return $result;
    }
    function getSekolahById($id = NULL){
        if($id != NULL){
            $where = "where s.id = '$id'";
        }else $where = "";
            
        $result = array();
        $query = $this->db->query("select s.*,tp.nama as tingkat_pendidikan from sekolah s join tingkat_pendidikan tp on s.id_tingkat_pendidikan=tp.id $where");
        if($query->num_rows() > 0){
           $result = $query->result_array();    
        }else $result = array();
        
        return $result;
    }
    function addSekolah($data){
        $insert = $this->db->insert($this->table,$data);
        if(!$insert){
            return FALSE;
        }else return TRUE;
    }
    function editSekolah($data){
        $this->db->where('id',$data['id']);
        $update = $this->db->update($this->table,$data);
        if(!$update){
            return FALSE;
        }else return TRUE;
    }
    function deleteSekolah($id){
        $delete = $this->db->delete($this->table,array('id'=>$id));
        if(!$delete){
            return FALSE;
        }else return TRUE;
    }
    function searchSekolah($param){
        $query = $this->db->query("select * from sekolah where locate('$param',nama) > 0 order by locate('$param',nama)");
        if($query->num_rows() > 0){
           $result = $query->result_array();    
        }else $result = array();
        
        return $result;
    }
}
?>
